# coding:utf-8

'''
    MGuide Framework
        
    Copyright (C) 2012  Alezy Oliveira Lima

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    @author: Alezy Oliveira Lima
    @contact: aol@ic.ufal.br
'''

class Evaluate:
    
    @classmethod
    def evaluate(cls, itemList, criteriaList):
        l = itemList
        for criteria in criteriaList:
            print criteria
            l = Evaluate.evaluateCriteria(l, criteria)
        return l
    
    @classmethod
    def evaluateCriteria(cls, itemList, criteria):
        attr = str(criteria.get('attr'))
        min = float(criteria.get('min'))
        max = float(criteria.get('max'))
        relevance = float(criteria.get('relevance'))
        proportional = criteria.get('proportional')
        
        if relevance not in range(0,101):
            print "relevance is not in range 0-100"
            return False
        
        for item in itemList:
            value = item.getValue(attr)
            if (len(itemList) == 1) or (max == min):
                evaluation = 100
            elif proportional:
                evaluation = ( (value-min)*100.0/(max-min)) * (relevance/100.0)
            else:
                evaluation = (100.0 - ((value-min)*100.0/(max-min))) * (relevance/100.0)
            item.setEvaluation(attr, evaluation)
        return itemList